﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;

namespace AutoParkingManagement
{
    public abstract class IPCameraHelper
    {
        /// <summary>
        /// Open video source
        /// </summary>
        /// <param name="player">player control</param>
        /// <param name="source">source: file or url stream</param>
        //public static void OpenVideoSource(IPACam.IpCameraPlayer player, IPACam.IVideoSource source, string[]databasePaths)
        //{
        //    // stop video source
        //    CloseVideoSource(player);            
        //    // start new video source            
        //    player.VideoSource = source;
        //    player.DataBasePaths = databasePaths;
        //    player.Start();
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="player"></param>
        /// <param name="source"></param>
        public static void OpenVideoSource(VideoSourcePlayer player, IVideoSource source)
        {
            // stop video source
            CloseVideoSource(player);

            // start new video source
            player.VideoSource = source;
            player.Start();
        }


        /// <summary>
        /// Close video source 
        /// </summary>
        /// <param name="player">player control</param>
        public static void CloseVideoSource(VideoSourcePlayer player)
        {
            if (player.VideoSource != null)
            {
                player.SignalToStop();
                player.WaitForStop();
                // wait ~ 3 seconds
                //for (int i = 0; i < 30; i++)
                //{
                //    if (!player.IsRunning)
                //        break;
                //    System.Threading.Thread.Sleep(100);
                //}

                if (player.IsRunning)
                {
                    player.Stop();
                }

                player.VideoSource = null;
            }
        }

        /// <summary>
        /// Close video source 
        /// </summary>
        /// <param name="player">player control</param>
        public static void CloseVideoSource(IPACam.IpCameraPlayer player)
        {
            if (player.VideoSource != null)
            {
                player.SignalToStop();
                player.WaitForStop();
                player.Stop();
            }
        }

        /// <summary>
        /// Capture image
        /// </summary>
        /// <param name="player">player control</param>
        /// <returns>image</returns>
        public static Image CaptureImage(IPACam.IpCameraPlayer player)
        {
            return player.GetCurrentVideoFrame();
        }

        public static Image CaptureImage(VideoSourcePlayer player)
        {
            return player.GetCurrentVideoFrame();
        }
    }
}
